package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.woniu.entity.CustomerInfos;
import com.woniu.entity.HouseHoldinfos;
import com.woniu.entity.Sales;
import com.woniu.service.IHouseHoldinfosService;
import com.woniu.service.ISalesService;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * </p>
 *
 * @author Gewang
 * @since 2021-04-01
 */
@RestController
public class SalesController {

    @Autowired
    ISalesService iSalesService;
    @Autowired
    IHouseHoldinfosService iHouseHoldinfosService;


    /**
     * 根据cid 查
     * @param cid
     * @return
     */
    @PostMapping("/sales/findByCid/{cid}")
    public ResultVO findByCid(@PathVariable("cid") Integer cid) {
        try {
            List<Sales> byCid = iSalesService.findByCid(cid);
            return ResultVO.success(byCid);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }
    /**
     * 新增销售表
     *
     * @param sales
     * @return
     */
    @PostMapping("/sales/save")
    public ResultVO sava(@RequestBody Sales sales) {
        try {
            int add = iSalesService.add(sales);
            if (add < 1) {
                return ResultVO.failure();
            }
            return ResultVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * 删除
     *
     * @param sid
     * @return
     */
    @PostMapping("/sales/deletes/{sid}")
    public ResultVO deletes(@PathVariable("sid") Integer sid) {
        try {
            int i = iSalesService.delete(sid);
            if (i < 1) {
                return ResultVO.failure();
            }
            return ResultVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }

    }

    /**
     * 根据sid修改信息
     *
     * @param sales
     * @return
     */
    @PostMapping("/sales/updateById")
    public ResultVO updateById(@RequestBody Sales sales) {
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>(sales);
        boolean falg = false;
        try {
            falg = iSalesService.update(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
        return (falg ? ResultVO.success() : ResultVO.failure());
    }

    /**
     * 全查
     *
     * @return
     */
    @PostMapping("/sales/findAll")
    public ResultVO findAll() {
        try {
            List<Sales> salesCustomerInfosHashMap = iSalesService.find();
            return ResultVO.success(salesCustomerInfosHashMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * 根据状态查
     *
     * @return
     */
    @PostMapping("/sales/findBystate/{state}")
    public ResultVO findBystate(@PathVariable("state") Integer state) {
        try {
            List<Sales> list = iSalesService.findByState(state);
            return ResultVO.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * 根据户id 查看详情
     */
    @PostMapping("/sales/findHouseHold/{hid}")
    public ResultVO findHouseHold(@PathVariable("hid") String hid) {
        try {
            ResultVO resultVO = iHouseHoldinfosService.findById(hid);

            Object data = resultVO.getData();
            System.out.println(data);

            return ResultVO.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * 查一个
     *
     * @return
     */
    @PostMapping("/sales/findById/{sid}")
    public ResultVO findById(@PathVariable("sid") Integer sid) {
        try {
            Sales sales = iSalesService.findById(sid);
            return ResultVO.success(sales);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * @param
     * @return
     */
    @PostMapping("/sales/findByName/{name}")
    public ResultVO findByName(@PathVariable("name") String name) {
        try {
            List<Sales> byName = iSalesService.findByName(name);
            return ResultVO.success(byName);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * 根据客户id查询Sales表
     *
     * @param cid
     * @return
     */
    @PostMapping("/sales/{customerID}")
    public ResultVO findByCustomerId(@PathVariable("customerID") Integer cid) {
        try {
            List<Sales> sales = iSalesService.findByCustomerId(cid);
            return ResultVO.success(sales);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failure();
        }
    }

    /**
     * 修改状态
     *
     * @param
     * @return
     */
    @PostMapping("/sales/updateState/{sid}/{state}")
    public ResultVO updateState(@PathVariable("sid") Integer sid, @PathVariable("state") Integer state) {
        try {
            int i = iSalesService.updateState(sid, state);
            if (i < 1) {
                return ResultVO.failure();
            }
            return ResultVO.success();
        } catch (Exception e) {

            e.printStackTrace();
            return ResultVO.failure();
        }
    }
}

